package com.javasm.controller.B2CController;

import com.javasm.common.http.AxiosResult;
import com.javasm.common.page.PageResult;
import com.javasm.controller.base.BaseController;
import com.javasm.domin.criteria.goodsCriteria.GoodsCriteria;
import com.javasm.domin.entity.goodsEntity.GdGoods;
import com.javasm.domin.entity.orderEntity.B2cOrder;
import com.javasm.domin.entity.orderEntity.B2cOrderGoods;
import com.javasm.domin.entity.orderEntity.B2cReceiver;
import com.javasm.domin.vo.goodsVo.GoodsVo;
import com.javasm.domin.vo.orderVo.OrderAndGoodAndReceiver;
import com.javasm.mapper.B2cOrderMapper.B2cReceiverMapper;
import com.javasm.mapper.B2cOrderMapper.OrderGoodsMapper;
import com.javasm.mapper.B2cOrderMapper.OrderMapper;
import com.javasm.mapper.goodsMapper.GdGoodsMapper;
import com.javasm.service.goodsService.GdGoodsService;
import jdk.internal.org.objectweb.asm.tree.FieldInsnNode;
import lombok.RequiredArgsConstructor;
import org.aspectj.weaver.ast.Var;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * @Author：liyangsong
 * @Version：1.0
 * @Date：2021/11/25-15:07
 * @Since:jdk1.8
 * @Description:
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/addOrder")
public class OrderAddController extends BaseController {

    private final GdGoodsService gdGoodsService;
    private final GdGoodsMapper gdGoodsMapper;
    private final OrderGoodsMapper orderGoodsMapper;
    private final OrderMapper orderMapper;
    private final B2cReceiverMapper b2cReceiverMapper;


    @RequestMapping("/add")
    public AxiosResult<Integer> add(@RequestBody OrderAndGoodAndReceiver orderAndGoodAndReceiver) {
        Integer i = null;
        //添加订单信息
        B2cOrder b2cOrder = new B2cOrder();
        B2cReceiver b2cReceiver = new B2cReceiver();
        B2cOrderGoods b2cOrderGoods = new B2cOrderGoods();

        b2cOrder.setOrderDate(orderAndGoodAndReceiver.getOrderDate());
        b2cOrder.setOrderAction(orderAndGoodAndReceiver.getOrderAction());
        b2cOrder.setDeliveryArea(orderAndGoodAndReceiver.getReceiverAdress());
        b2cOrder.setOrderType(orderAndGoodAndReceiver.getOrderType());
//        b2cOrder.setReceiverId(Math.toIntExact(id));
        b2cOrder.setDeliveryArea(orderAndGoodAndReceiver.getDeliveryArea());
        b2cOrder.setOrderStatus(orderAndGoodAndReceiver.getOrderStatus());
        b2cOrder.setDeliveryInsurance(orderAndGoodAndReceiver.getDeliveryInsurance());
        b2cOrder.setDeliveryStyle(orderAndGoodAndReceiver.getDeliveryStyle());
        b2cOrder.setDeliverySamecity(orderAndGoodAndReceiver.getDeliverySamecity());
        b2cOrder.setDeliveryCost(orderAndGoodAndReceiver.getDeliveryCost());
        b2cOrder.setVipId(orderAndGoodAndReceiver.getVipId());
        b2cOrder.setPaymentMethodId(orderAndGoodAndReceiver.getPaymentMethodId());
        b2cOrder.setDeliveryTime(orderAndGoodAndReceiver.getDeliveryTime());
        b2cOrder.setBusinessType(orderAndGoodAndReceiver.getBusinessType());
        i = orderMapper.insert(b2cOrder);
        Long orderId = b2cOrder.getId();//得到订单id
        System.out.println(orderId);
        //添加收货人信息
        b2cReceiver.setOrderId(orderId);
        b2cReceiver.setPayName(orderAndGoodAndReceiver.getPayName());
        b2cReceiver.setReceiverAdress(orderAndGoodAndReceiver.getReceiverAdress());
        b2cReceiver.setReceiverName(orderAndGoodAndReceiver.getReceiverName());
        b2cReceiver.setReceiverPhone(orderAndGoodAndReceiver.getReceiverPhone());
        b2cReceiver.setReceiverEmail(orderAndGoodAndReceiver.getReceiverEmail());
        b2cReceiver.setReceiverNotation(orderAndGoodAndReceiver.getReceiverNotation());
        b2cReceiver.setReceiverTelephone(orderAndGoodAndReceiver.getReceiverTelephone());
        b2cReceiver.setPostalCode(orderAndGoodAndReceiver.getPostalCode());
        b2cReceiver.setVipId(orderAndGoodAndReceiver.getVipId());
        b2cReceiver.setSelfMentiontime(orderAndGoodAndReceiver.getSelfMentiontime());
        i = b2cReceiverMapper.insert(b2cReceiver);
        Long id = b2cReceiver.getId();

        //添加商品信息
        b2cOrderGoods.setOrderProductName(orderAndGoodAndReceiver.getOrderProductName());
        b2cOrderGoods.setGoodsId(orderAndGoodAndReceiver.getGoodsId());
        b2cOrderGoods.setOrderProductPrice(orderAndGoodAndReceiver.getOrderProductPrice());
        b2cOrderGoods.setBrandId(orderAndGoodAndReceiver.getBrandId());
        b2cOrderGoods.setColorId(orderAndGoodAndReceiver.getColorId());
        b2cOrderGoods.setOrderProductMoney(orderAndGoodAndReceiver.getOrderProductMoney());
        b2cOrderGoods.setOrderId(Math.toIntExact(orderId));
        b2cOrderGoods.setModelId(orderAndGoodAndReceiver.getModelId());
        b2cOrderGoods.setOrderProductNumber(orderAndGoodAndReceiver.getOrderProductNumber());
        b2cOrderGoods.setTotal(orderAndGoodAndReceiver.getOrderProductNumber());
        i = orderGoodsMapper.insert(b2cOrderGoods);
        return AxiosResult.success(i);
    }


    /**
     * 查询所有
     *
     * @param goodsCriteria
     * @return
     */
    @GetMapping("/findList")
    public AxiosResult<PageResult<GoodsVo>> list(GoodsCriteria goodsCriteria) {
        PageResult<GoodsVo> goodsVoPageResult = gdGoodsService.searchPage(goodsCriteria);
//        System.out.println(goodsVoPageResult);
        return AxiosResult.success(goodsVoPageResult);
    }

    /**
     * 根据id得到商品
     *
     * @param id
     * @return
     */
    @RequestMapping("/findGood/{id}")
    public AxiosResult<GdGoods> find(@PathVariable Serializable id) {
        GdGoods gdGoods = gdGoodsMapper.selectById(id);
        B2cOrder b2cOrder = new B2cOrder();
        B2cOrderGoods b2cOrderGoods = new B2cOrderGoods();
        return AxiosResult.success(gdGoods);
    }

    /**
     * 根据商品id 批量得到商品
     *
     * @param idList
     * @return
     */
    @RequestMapping("/findGoodList/{idList}")
    public AxiosResult<List<GdGoods>> findList(@PathVariable List<Serializable> idList) {
        List<GdGoods> gdGoods = gdGoodsMapper.selectBatchIds(idList);
        return AxiosResult.success(gdGoods);
    }

    /**
     * 添加订单商品
     *
     * @param b2cOrderGoods
     * @return
     */
    @PostMapping("/addOrderGood")
    public AxiosResult<Integer> show(@RequestBody B2cOrderGoods b2cOrderGoods) {
        int insert = orderGoodsMapper.insert(b2cOrderGoods);
        return AxiosResult.success(insert);

    }


    @RequestMapping("/addList")
    public AxiosResult<Integer> addList(@RequestBody OrderAndGoodAndReceiver orderAndGoodAndReceiver) {
        Integer i = null;
        //添加订单信息
        B2cOrder b2cOrder = new B2cOrder();
        B2cReceiver b2cReceiver = new B2cReceiver();

        b2cOrder.setOrderDate(orderAndGoodAndReceiver.getOrderDate());
        b2cOrder.setOrderAction(orderAndGoodAndReceiver.getOrderAction());
        b2cOrder.setDeliveryArea(orderAndGoodAndReceiver.getReceiverAdress());
        b2cOrder.setOrderType(orderAndGoodAndReceiver.getOrderType());
//        b2cOrder.setReceiverId(Math.toIntExact(id));
        b2cOrder.setDeliveryArea(orderAndGoodAndReceiver.getDeliveryArea());
        b2cOrder.setOrderStatus(orderAndGoodAndReceiver.getOrderStatus());
        b2cOrder.setDeliveryInsurance(orderAndGoodAndReceiver.getDeliveryInsurance());
        b2cOrder.setDeliveryStyle(orderAndGoodAndReceiver.getDeliveryStyle());
        b2cOrder.setDeliverySamecity(orderAndGoodAndReceiver.getDeliverySamecity());
        b2cOrder.setDeliveryCost(orderAndGoodAndReceiver.getDeliveryCost());
        b2cOrder.setVipId(orderAndGoodAndReceiver.getVipId());
        b2cOrder.setPaymentMethodId(orderAndGoodAndReceiver.getPaymentMethodId());
        b2cOrder.setDeliveryTime(orderAndGoodAndReceiver.getDeliveryTime());
        b2cOrder.setBusinessType(orderAndGoodAndReceiver.getBusinessType());
        i = orderMapper.insert(b2cOrder);
        Long orderId = b2cOrder.getId();//得到订单id
        System.out.println(orderId);
        //添加收货人信息
        b2cReceiver.setOrderId(orderId);
        b2cReceiver.setPayName(orderAndGoodAndReceiver.getPayName());
        b2cReceiver.setReceiverAdress(orderAndGoodAndReceiver.getReceiverAdress());
        b2cReceiver.setReceiverName(orderAndGoodAndReceiver.getReceiverName());
        b2cReceiver.setReceiverPhone(orderAndGoodAndReceiver.getReceiverPhone());
        b2cReceiver.setReceiverEmail(orderAndGoodAndReceiver.getReceiverEmail());
        b2cReceiver.setReceiverNotation(orderAndGoodAndReceiver.getReceiverNotation());
        b2cReceiver.setReceiverTelephone(orderAndGoodAndReceiver.getReceiverTelephone());
        b2cReceiver.setPostalCode(orderAndGoodAndReceiver.getPostalCode());
        b2cReceiver.setVipId(orderAndGoodAndReceiver.getVipId());
        b2cReceiver.setSelfMentiontime(orderAndGoodAndReceiver.getSelfMentiontime());
        i = b2cReceiverMapper.insert(b2cReceiver);
        Long id = b2cReceiver.getId();
        //循环添加商品
        List<B2cOrderGoods> b2cOrderGoodsList = orderAndGoodAndReceiver.getB2cOrderGoodsList();
        for (int i1 = 0; i1 < b2cOrderGoodsList.size(); i1++) {
            B2cOrderGoods b2cOrderGoods = b2cOrderGoodsList.get(i1);
            b2cOrderGoods.setOrderId(Math.toIntExact(orderId));
            i = orderGoodsMapper.insert(b2cOrderGoods);
            System.out.println(b2cOrderGoods);
        }
        return AxiosResult.success(i);
    }
}
